﻿using System;
using Silver3D.Math;

namespace Silver3D.Graphics
{
    /// <summary>
    /// Defines a "look at" transform from a position, a target, and an up vector.
    /// </summary>
    public class LookAtTransform : MatrixTransform
    {
        private Vec3 eye = Vec3.Zero;
        private Vec3 target = Vec3.XAxis;
        private Vec3 up = Vec3.ZAxis;

        /// <summary>
        /// Gets or sets the Eye position.
        /// Defines the translation of the transformation.
        /// </summary>
        public Vec3 Eye
        {
            get { return eye; }
            set
            {
                eye = value;
                IsDirty = true;
            }
        }

        /// <summary>
        /// Gets of sets the Target position.
        /// Defines the yaw and pitch of the rotation.
        /// </summary>
        public Vec3 Target
        {
            get { return target; }
            set
            {
                target = value;
                IsDirty = true;
            }
        }

        /// <summary>
        /// The Up vector (sky direction).
        /// Defines the roll of the rotation.
        /// </summary>
        public Vec3 Up
        {
            get { return up; }
            set
            {
                up = value;
                IsDirty = true;
            }
        }

        public LookAtTransform(Scene scene, string id)
            : base(scene, id)
        {
        }

        public LookAtTransform(Scene scene, string id, Node parent)
            : base(scene, id, parent)
        {
        }

        public LookAtTransform(Scene scene, string id, Node parent, Vec3 eye, Vec3 target, Vec3 up)
            : base(scene, id, parent)
        {
            Eye = eye;
            Target = target;
            Up = up;
        }

        /// <summary>
        /// Updates the CachedMatrix member to a LookAt matrix.
        /// </summary>
        protected override void UpdateMatrix()
        {
            CachedMatrix = Matrix44.CreateLookAt(eye, target, up);
        }
    }
}
